package com.yuneec.fly.views;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.ClipDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;

import com.yuneec.fly.R;

public class VernierView extends View {
    private static final int VERNIER_X = 0;
    private static final int VERNIER_Y = 0;
    private static final int CURSOR_X0 = 27;
    private static final int CURSOR_Y0 = 84;
    private Paint mPaint = new Paint();
    private Cursor mCursor = new Cursor();

    class Cursor {
        float x;
        float y;
        int value;

    }

    public VernierView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public VernierView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public VernierView(Context context) {
        super(context);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawBackground(canvas);
        drawCursor(canvas);
    }

    private void drawBackground(Canvas canvas) {
        Bitmap view_bg = BitmapFactory.decodeResource(getResources(), R.drawable.hwm_vernier);
        mPaint.setAntiAlias(true);
        canvas.drawBitmap(view_bg, VERNIER_X, VERNIER_Y, mPaint);
    }

    private void drawCursor(Canvas canvas) {

        ClipDrawable cursorBarTop = (ClipDrawable) getResources().getDrawable(R.drawable.hwm_cursorbar_top);
        ClipDrawable cursorBarBottom = (ClipDrawable) getResources().getDrawable(R.drawable.hwm_cursorbar_bottom);

        Drawable cursorIcon = getResources().getDrawable(R.drawable.hwm_cursor);
        mPaint.setAntiAlias(true);

        mCursor.x = CURSOR_X0;
        mCursor.y = (float) (CURSOR_Y0 - mCursor.value * 0.515); //(imageHeight(169)-offset(33)*2)/value(200)=0.515

        float cursorIconLeft = mCursor.x - 2;
        float cursorIconTop = mCursor.y - cursorIcon.getIntrinsicHeight() / 2;

        int level = (int) (mCursor.value * 0.515 * 200);//MaxLevel(10000)/imageHeight(50)=200
        if (level > 0) {
            /*Set level of cursor-bar,display half of top*/
            cursorBarTop.setLevel(level);
            cursorBarBottom.setLevel(0);

        } else if (level < 0) {
			/*Set level of cursor-bar, display half of bottom*/
            cursorBarTop.setLevel(0);
            cursorBarBottom.setLevel(Math.abs(level));

        } else {
			/*Set level of cursor-bar, display*/
            cursorBarTop.setLevel(0);
            cursorBarBottom.setLevel(0);
        }
		
		/*Draw the top of cursor-bar*/
        cursorBarTop.setBounds(CURSOR_X0, (CURSOR_Y0 - cursorBarTop.getIntrinsicHeight()),
                (CURSOR_X0 + cursorBarTop.getIntrinsicWidth()), CURSOR_Y0);
        cursorBarTop.draw(canvas);
		
		/*Draw the bottom of cursor-bar*/
        cursorBarBottom.setBounds(CURSOR_X0, (CURSOR_Y0 + 1),
                (CURSOR_X0 + cursorBarBottom.getIntrinsicWidth()), (CURSOR_Y0 + cursorBarBottom.getIntrinsicHeight() + 1));
        cursorBarBottom.draw(canvas);

        //Draw cursor-icon
        Bitmap bm_cur = BitmapFactory.decodeResource(getResources(), R.drawable.hwm_cursor);
        canvas.drawBitmap(bm_cur, cursorIconLeft, cursorIconTop, mPaint);
    }

    public void setValue(int value) {
        if (mCursor.value != value) {
            mCursor.value = value;
            invalidate();
        }
    }

    public int getValue() {
        return mCursor.value;
    }

}
